/*
 * @lc app=leetcode.cn id=61 lang=golang
 *
 * [61] 旋转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func rotateRight(head *ListNode, k int) *ListNode {
	l := 0
	current := head
	for current != nil {
		l++
		current = current.Next
	}

	offset := k % l
	p, q := head, head
	for i := 0; i < offset; i++ {
		q = q.Next
	}

	for q.Next != nil {
		p = p.Next
		q = q.Next
	}

	o := p.Next
	p.Next = nil
	return o
}

// @lc code=end

